<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActiveRecord::Serialization</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActiveRecord::Serialization</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../files/vendor/rails/activerecord/lib/active_record/serialization_rb.html">
                vendor/rails/activerecord/lib/active_record/serialization.rb
                </a>
        <br />
                <a href="../../files/vendor/rails/activerecord/lib/active_record/serializers/json_serializer_rb.html">
                vendor/rails/activerecord/lib/active_record/serializers/json_serializer.rb
                </a>
        <br />
                <a href="../../files/vendor/rails/activerecord/lib/active_record/serializers/xml_serializer_rb.html">
                vendor/rails/activerecord/lib/active_record/serializers/xml_serializer.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">



   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M001136">from_json</a>&nbsp;&nbsp;
      <a href="#M001138">from_xml</a>&nbsp;&nbsp;
      <a href="#M001135">to_json</a>&nbsp;&nbsp;
      <a href="#M001137">to_xml</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M001136" class="method-detail">
        <a name="M001136"></a>

        <div class="method-heading">
          <a href="#M001136" class="method-signature">
          <span class="method-name">from_json</span><span class="method-args">(json)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001136-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001136-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/serializers/json_serializer.rb, line 60</span>
60:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">from_json</span>(<span class="ruby-identifier">json</span>)
61:       <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">attributes</span> = <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">JSON</span>.<span class="ruby-identifier">decode</span>(<span class="ruby-identifier">json</span>)
62:       <span class="ruby-keyword kw">self</span>
63:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001138" class="method-detail">
        <a name="M001138"></a>

        <div class="method-heading">
          <a href="#M001138" class="method-signature">
          <span class="method-name">from_xml</span><span class="method-args">(xml)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001138-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001138-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/serializers/xml_serializer.rb, line 128</span>
128:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">from_xml</span>(<span class="ruby-identifier">xml</span>)
129:       <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">attributes</span> = <span class="ruby-constant">Hash</span>.<span class="ruby-identifier">from_xml</span>(<span class="ruby-identifier">xml</span>).<span class="ruby-identifier">values</span>.<span class="ruby-identifier">first</span>
130:       <span class="ruby-keyword kw">self</span>
131:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001135" class="method-detail">
        <a name="M001135"></a>

        <div class="method-heading">
          <a href="#M001135" class="method-signature">
          <span class="method-name">to_json</span><span class="method-args">(options = {})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns a JSON string representing the model. Some configuration is
available through <tt>options</tt>.
</p>
<p>
Without any <tt>options</tt>, the returned JSON string will include all the
model&#8216;s attributes. For example:
</p>
<pre>
  konata = User.find(1)
  konata.to_json

  {&quot;id&quot;: 1, &quot;name&quot;: &quot;Konata Izumi&quot;, &quot;age&quot;: 16,
   &quot;created_at&quot;: &quot;2006/08/01&quot;, &quot;awesome&quot;: true}
</pre>
<p>
The :only and :except options can be used to limit the attributes included,
and work similar to the attributes method. For example:
</p>
<pre>
  konata.to_json(:only =&gt; [ :id, :name ])

  {&quot;id&quot;: 1, &quot;name&quot;: &quot;Konata Izumi&quot;}

  konata.to_json(:except =&gt; [ :id, :created_at, :age ])

  {&quot;name&quot;: &quot;Konata Izumi&quot;, &quot;awesome&quot;: true}
</pre>
<p>
To include any methods on the model, use :methods.
</p>
<pre>
  konata.to_json(:methods =&gt; :permalink)

  {&quot;id&quot;: 1, &quot;name&quot;: &quot;Konata Izumi&quot;, &quot;age&quot;: 16,
   &quot;created_at&quot;: &quot;2006/08/01&quot;, &quot;awesome&quot;: true,
   &quot;permalink&quot;: &quot;1-konata-izumi&quot;}
</pre>
<p>
To include associations, use :include.
</p>
<pre>
  konata.to_json(:include =&gt; :posts)

  {&quot;id&quot;: 1, &quot;name&quot;: &quot;Konata Izumi&quot;, &quot;age&quot;: 16,
   &quot;created_at&quot;: &quot;2006/08/01&quot;, &quot;awesome&quot;: true,
   &quot;posts&quot;: [{&quot;id&quot;: 1, &quot;author_id&quot;: 1, &quot;title&quot;: &quot;Welcome to the weblog&quot;},
             {&quot;id&quot;: 2, author_id: 1, &quot;title&quot;: &quot;So I was thinking&quot;}]}
</pre>
<p>
2nd level and higher order associations work as well:
</p>
<pre>
  konata.to_json(:include =&gt; { :posts =&gt; {
                                 :include =&gt; { :comments =&gt; {
                                               :only =&gt; :body } },
                                 :only =&gt; :title } })

  {&quot;id&quot;: 1, &quot;name&quot;: &quot;Konata Izumi&quot;, &quot;age&quot;: 16,
   &quot;created_at&quot;: &quot;2006/08/01&quot;, &quot;awesome&quot;: true,
   &quot;posts&quot;: [{&quot;comments&quot;: [{&quot;body&quot;: &quot;1st post!&quot;}, {&quot;body&quot;: &quot;Second!&quot;}],
              &quot;title&quot;: &quot;Welcome to the weblog&quot;},
             {&quot;comments&quot;: [{&quot;body&quot;: &quot;Don't think too hard&quot;}],
              &quot;title&quot;: &quot;So I was thinking&quot;}]}
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001135-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001135-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/serializers/json_serializer.rb, line 56</span>
56:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">to_json</span>(<span class="ruby-identifier">options</span> = {})
57:       <span class="ruby-constant">JsonSerializer</span>.<span class="ruby-identifier">new</span>(<span class="ruby-keyword kw">self</span>, <span class="ruby-identifier">options</span>).<span class="ruby-identifier">to_s</span>
58:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001137" class="method-detail">
        <a name="M001137"></a>

        <div class="method-heading">
          <a href="#M001137" class="method-signature">
          <span class="method-name">to_xml</span><span class="method-args">(options = {}, &amp;block)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Builds an XML document to represent the model. Some configuration is
available through <tt>options</tt>, however more complicated cases should
override ActiveRecord&#8216;s <a
href="Serialization.html#M001137">to_xml</a>.
</p>
<p>
By default the generated XML document will include the processing
instruction and all object&#8216;s attributes. For example:
</p>
<pre>
  &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;topic&gt;
    &lt;title&gt;The First Topic&lt;/title&gt;
    &lt;author-name&gt;David&lt;/author-name&gt;
    &lt;id type=&quot;integer&quot;&gt;1&lt;/id&gt;
    &lt;approved type=&quot;boolean&quot;&gt;false&lt;/approved&gt;
    &lt;replies-count type=&quot;integer&quot;&gt;0&lt;/replies-count&gt;
    &lt;bonus-time type=&quot;datetime&quot;&gt;2000-01-01T08:28:00+12:00&lt;/bonus-time&gt;
    &lt;written-on type=&quot;datetime&quot;&gt;2003-07-16T09:28:00+1200&lt;/written-on&gt;
    &lt;content&gt;Have a nice day&lt;/content&gt;
    &lt;author-email-address&gt;david@loudthinking.com&lt;/author-email-address&gt;
    &lt;parent-id&gt;&lt;/parent-id&gt;
    &lt;last-read type=&quot;date&quot;&gt;2004-04-15&lt;/last-read&gt;
  &lt;/topic&gt;
</pre>
<p>
This behavior can be controlled with :only, :except, :skip_instruct,
:skip_types and :dasherize. The :only and :except options are the same as
for the attributes method. The default is to dasherize all column names, to
disable this, set :dasherize to false. To not have the column type included
in the XML output, set :skip_types to true.
</p>
<p>
For instance:
</p>
<pre>
  topic.to_xml(:skip_instruct =&gt; true, :except =&gt; [ :id, :bonus_time, :written_on, :replies_count ])

  &lt;topic&gt;
    &lt;title&gt;The First Topic&lt;/title&gt;
    &lt;author-name&gt;David&lt;/author-name&gt;
    &lt;approved type=&quot;boolean&quot;&gt;false&lt;/approved&gt;
    &lt;content&gt;Have a nice day&lt;/content&gt;
    &lt;author-email-address&gt;david@loudthinking.com&lt;/author-email-address&gt;
    &lt;parent-id&gt;&lt;/parent-id&gt;
    &lt;last-read type=&quot;date&quot;&gt;2004-04-15&lt;/last-read&gt;
  &lt;/topic&gt;
</pre>
<p>
To include first level associations use :include
</p>
<pre>
  firm.to_xml :include =&gt; [ :account, :clients ]

  &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;firm&gt;
    &lt;id type=&quot;integer&quot;&gt;1&lt;/id&gt;
    &lt;rating type=&quot;integer&quot;&gt;1&lt;/rating&gt;
    &lt;name&gt;37signals&lt;/name&gt;
    &lt;clients type=&quot;array&quot;&gt;
      &lt;client&gt;
        &lt;rating type=&quot;integer&quot;&gt;1&lt;/rating&gt;
        &lt;name&gt;Summit&lt;/name&gt;
      &lt;/client&gt;
      &lt;client&gt;
        &lt;rating type=&quot;integer&quot;&gt;1&lt;/rating&gt;
        &lt;name&gt;Microsoft&lt;/name&gt;
      &lt;/client&gt;
    &lt;/clients&gt;
    &lt;account&gt;
      &lt;id type=&quot;integer&quot;&gt;1&lt;/id&gt;
      &lt;credit-limit type=&quot;integer&quot;&gt;50&lt;/credit-limit&gt;
    &lt;/account&gt;
  &lt;/firm&gt;
</pre>
<p>
To include any methods on the object(s) being called use :methods
</p>
<pre>
  firm.to_xml :methods =&gt; [ :calculated_earnings, :real_earnings ]

  &lt;firm&gt;
    # ... normal attributes as shown above ...
    &lt;calculated-earnings&gt;100000000000000000&lt;/calculated-earnings&gt;
    &lt;real-earnings&gt;5&lt;/real-earnings&gt;
  &lt;/firm&gt;
</pre>
<p>
To call any Proc&#8216;s on the object(s) use :procs. The Proc&#8216;s are
passed a modified version of the options hash that was given to <a
href="Serialization.html#M001137">to_xml</a>.
</p>
<pre>
  proc = Proc.new { |options| options[:builder].tag!('abc', 'def') }
  firm.to_xml :procs =&gt; [ proc ]

  &lt;firm&gt;
    # ... normal attributes as shown above ...
    &lt;abc&gt;def&lt;/abc&gt;
  &lt;/firm&gt;
</pre>
<p>
Alternatively, you can also just yield the builder object as part of the <a
href="Serialization.html#M001137">to_xml</a> call:
</p>
<pre>
  firm.to_xml do |xml|
    xml.creator do
      xml.first_name &quot;David&quot;
      xml.last_name &quot;Heinemeier Hansson&quot;
    end
  end

  &lt;firm&gt;
    # ... normal attributes as shown above ...
    &lt;creator&gt;
      &lt;first_name&gt;David&lt;/first_name&gt;
      &lt;last_name&gt;Heinemeier Hansson&lt;/last_name&gt;
    &lt;/creator&gt;
  &lt;/firm&gt;
</pre>
<p>
You can override the <a href="Serialization.html#M001137">to_xml</a> method
in your <a href="Base.html">ActiveRecord::Base</a> subclasses if you need
to. The general form of doing this is
</p>
<pre>
  class IHaveMyOwnXML &lt; ActiveRecord::Base
    def to_xml(options = {})
      options[:indent] ||= 2
      xml = options[:builder] ||= Builder::XmlMarkup.new(:indent =&gt; options[:indent])
      xml.instruct! unless options[:skip_instruct]
      xml.level_one do
        xml.tag!(:second_level, 'content')
      end
    end
  end
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001137-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001137-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/serializers/xml_serializer.rb, line 123</span>
123:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">to_xml</span>(<span class="ruby-identifier">options</span> = {}, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
124:       <span class="ruby-identifier">serializer</span> = <span class="ruby-constant">XmlSerializer</span>.<span class="ruby-identifier">new</span>(<span class="ruby-keyword kw">self</span>, <span class="ruby-identifier">options</span>)
125:       <span class="ruby-identifier">block_given?</span> <span class="ruby-value">? </span><span class="ruby-identifier">serializer</span>.<span class="ruby-identifier">to_s</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-operator">:</span> <span class="ruby-identifier">serializer</span>.<span class="ruby-identifier">to_s</span>
126:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>